package com.smartens.boit.adapter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.lifecycle.ViewModelStoreOwner;
import androidx.recyclerview.widget.RecyclerView;

import com.smartens.boit.R;
import com.smartens.boit.activity.viewmodel.MainActivityViewMode;
import com.smartens.boit.bean.ControlBean;
import com.smartens.boit.bean.FaultBean;
import com.smartens.boit.fragment.SecondFragment;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description 最新报警适配器
 * @Author songtt
 * @Date 2021/3/4
 */

public class LatestAdapter extends RecyclerView.Adapter<LatestAdapter.ViewHolder> {
    private List<FaultBean> mList ;
    private OnItemClickListener onItemClickListener;
    private MainActivityViewMode mainActivityViewMode;
    public LatestAdapter(List<FaultBean> list, SecondFragment secondFragment) {
        mList = list;
        mainActivityViewMode=secondFragment.mainActivityViewMode;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //将我们自定义的item布局R.layout.item_one转换为View
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_latest, parent, false);
        //将view传递给我们自定义的ViewHolder
        ViewHolder holder = new ViewHolder(view);
        //返回这个MyHolder实体
        return holder;
    }

    /**
     * 通过方法提供的ViewHolder，将数据绑定到ViewHolder中
     *
     * @param holder
     * @param position
     */
    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.name.setText(mList.get(position).getName());
        holder.number.setText("设备编号 " +mList.get(position).getNumber());
        holder.date.setText(mList.get(position).getDateTime());
        //小红点是否显示
        if (mList.get(position).getStatus() == 0){
            holder.latest.setVisibility(View.VISIBLE);
            holder.deal.setVisibility(View.GONE);
            mainActivityViewMode.getIsnewMessage().setValue(true);
        }else {
            holder.latest.setVisibility(View.GONE);
            holder.deal.setVisibility(View.VISIBLE);
            mainActivityViewMode.getIsnewMessage().setValue(false);
        }

        holder.linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(v, holder.getAdapterPosition());
                }
            }
        });

    }

    /**
     * 获取数据源总的条数
     *
     * @return
     */
    @Override
    public int getItemCount() {
        Log.v("getItemCount",mList.size()+"");
        return mList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        LinearLayout linearLayout;
        TextView name;
        TextView number;
        TextView date;
        TextView latest;
        TextView deal;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            linearLayout = itemView.findViewById(R.id.lin);
            name = itemView.findViewById(R.id.name);
            number = itemView.findViewById(R.id.number);
            date = itemView.findViewById(R.id.time);
            latest = itemView.findViewById(R.id.tv_latest);
            deal = itemView.findViewById(R.id.tv_deal);
        }
    }

    /**
     * 供外部调用设置监听
     *
     * @param onItemClickListener
     */
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }


    /**
     * 自定义的接口
     */
    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }
public  void setData(List data)
{
    mList=data;
}

}
